package com.jinhua.admin.service.impl;

import cn.common.annotation.BasicRetry;
import cn.common.dto.RetryRequestParamDTO;
import cn.common.exceptions.BasicRetryException;
import com.jinhua.admin.dao.UserDao;
import com.jinhua.admin.entity.UserPo;
import com.jinhua.admin.service.UserService;
import com.jinhua.admin.vo.UserVo;
import com.jinhua.config.BeanCopierUtil;
import com.jinhua.config.IdUtil;
import com.jinhua.config.PaginationRequest;
import com.jinhua.config.PaginationResponse;
import com.util.config.HttpResult;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.RandomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 用户会员表的service层
 */

@Service
@Slf4j
public class UserServiceImpl implements UserService  {
    private final long ERROR_LONG = 0L;
    private final int ERROR_INT = 0;
    @Autowired
    private UserDao userDao;

    @Transactional
    @Override
    public long saveUser(UserVo userVo) {
        if (userVo == null) {
            log.warn("操作用户会员表参数为空");
            return ERROR_LONG;
        }

        UserPo userPo = userVo.copyUserPo();
        if (userPo.getId() == null) {
            log.info("start添加用户会员表={}", userPo);
            int result = userDao.saveUserIfNotExist(userPo);
            return result != 0 ? userPo.getId() : ERROR_LONG;
        } else {
            log.info("start修改用户会员表={}", userPo.getId());
            return userDao.modUser(userPo);
        }
    }

    @Override
    public int delUserByIds(Long... ids) {
        if (ids == null || ids.length == 0) {
            log.warn("删除用户会员表,ids集合为空");
            return ERROR_INT;
        }
        log.warn("start删除Id集合={},用户会员表", Arrays.asList(ids));
        return userDao.delUserByIds(ids);
    }

    @Override
    public UserVo findUserById(Long id) {
        if (id == null || id <= 0L) {
            log.warn("根据Id查找用户会员表,所传Id不符合规范");
            return new UserVo();
        }
        log.info("start根据Id={}查找用户会员表", id);
        UserPo userPo = userDao.findUserById(id);
        return userPo == null ? new UserVo() : userPo.copyUserVo();
    }

    @Override
    public PaginationResponse<UserVo> findUserByAll(PaginationRequest<UserVo> request) {
        PaginationResponse<UserVo> response = new PaginationResponse<UserVo>();
        if (request == null) {
            log.warn("根据条件查找用户会员表,参数不对");
            return response;
        }
        log.info("start根据条件查找用户会员表={}", request);
        List<UserVo> userVos = new ArrayList<>();
        try {
            List<UserPo> userPos = userDao.findUserByAll(request.getQueryCriteria().copyUserPo(), request.getPageNum(), request.getPageSize());
            userVos = BeanCopierUtil.copyList(userPos, UserPo.class, UserVo.class);
        } catch (Exception e) {
            log.error("UserServiceImpl.findOrderByAll error:{}", e.getMessage(), e);
            throw new RuntimeException(e);
        }
        response.setDataList(userVos);
        response.setPageSize(request.getPageSize());
        response.setPageNum(request.getPageNum());
        response.setTotalCount(IdUtil.getTotal());
        return response;
    }

    @Override
    @BasicRetry(retryMaxCount = 3, enableDelayRetry = false,retryIntervel = 30)
    public HttpResult<String> doWork(RetryRequestParamDTO<UserVo> retryRequest) throws Exception {
        UserVo openApiPostRequest = retryRequest.getRequestBody();
        int i = RandomUtils.nextInt(0,11);
        log.info("随机生成的数:{}",i);
        if (i < 2) {
            log.info("为0,抛出参数异常.");
            throw new BasicRetryException("参数异常");
        }else if (i  < 5){
            log.info("为1,返回true.");
            return HttpResult.succ("1");
        }else if (i < 7){
            log.info("为2,返回false.");
            return HttpResult.succ("1");
        }else{
            //为其他
            log.info("大于2,抛出自定义异常.");
            throw new BasicRetryException("大于2,抛出自定义异常");
        }
    }
}
